 --[[
任务模块回调处理函数 对外接口
taskMonsterDieCB	怪物被打死回调
taskRoleDieCB		玩家被打死回调
taskCollectCB		采集物被采集回调
taskUseItemCB		使用道具回调
taskRefineCB		神炉回调
taskFriendCB		好友互动回调
taskInteractCB		功能交互回调
taskCopyEnterCB		副本进入回调
taskCopyCB			副本完成回调
taskReachPlaceCB	搜索任务到达某区域
--]]

local Util = require("common.Util")
local Msg = require("core.Msg")
local Map = require("core.Map")
local Obj = require("core.Obj")
local ObjMonster = require("core.ObjMonster")
local TaskDefine = require("task.Define")
local TaskAction = require("task.TaskAction")
local TaskHelp = require("task.TaskHelp")
local TaskLogic = require("task.TaskLogic")
local TaskHandler = require("task.HandlerL")

local function taskChangeFinish(human, taskID, canFinish, record)
  record = record or TaskLogic.getTaskRecord(human, taskID)      
	local gcCanFinishMsg = Msg.gc.GC_TASK_CAN_FINISH_STATUS
	gcCanFinishMsg.taskID = taskID
	if canFinish == true then	  
		if record.finishFlag ~= TaskDefine.TASK_FINISH_FLAG_NO_CHECK then
		    record.finishFlag = TaskDefine.TASK_FINISH_FLAG_CAN_FINISH
		end
		gcCanFinishMsg.canFinish = 1
		
		-- 播放动画
		local taskConfig = TaskHelp.getTaskConfig(taskID)
		TaskAction.playMovie(human, taskConfig.movie2)
	else
		if record.finishFlag ~= TaskDefine.TASK_FINISH_FLAG_NO_CHECK then
        record.finishFlag = TaskDefine.TASK_FINISH_FLAG_NONE
    end
		gcCanFinishMsg.canFinish = 0
	end
	Msg.send(gcCanFinishMsg, human.fd)	
	
  if gcCanFinishMsg.canFinish == 1 then
    local taskConfig = TaskHelp.getTaskConfig(taskID)
    if taskConfig.autoComplete == 1 then
      local msgTemp = {taskID = taskID}
      TaskHandler.CG_TASK_FINISH(human, msgTemp)
    end
  end
end

local function updateTaskObjNum(human, record, taskID, targetID, needNum, curVal)
  local param
  for i = 0, record.paramsLen - 1 do
      if record.params[i].target == targetID then
            param = record.params[i]
            break 
      end
  end
	if param == nil then
		  if  record.paramsLen >=10 then assert() end
		  param = record.params[record.paramsLen]
		  record.paramsLen = record.paramsLen + 1
		  param.target = targetID
		  param.num = 0
	end
	if param.num < needNum then
	  if curVal then  -- 设置当前值
	     param.num = curVal > needNum and needNum or curVal	     
	  else
		   param.num = param.num + 1
		end
		local gcTaskObjNumMsg = Msg.gc.GC_TASK_OBJ_NUM
		gcTaskObjNumMsg.taskID = taskID
		gcTaskObjNumMsg.objTypeID = targetID
		gcTaskObjNumMsg.objNum = param.num
		Msg.send(gcTaskObjNumMsg, human.fd)		
		return true
	end
end


local function isTargetFinish(human,record, targetID, needNum)
  for i = 0, record.paramsLen - 1 do
      if record.params[i].target == targetID then
         return record.params[i].num >= needNum
      end
  end
end

local function updateTaskObjNumSpecial(human, record, taskID, targetID, needNum)
	local param
  for i = 0, record.paramsLen - 1 do
      if record.params[i].target == targetID then
            param = record.params[i]
            break 
      end
  end
  if param == nil then
      if  record.paramsLen >=10 then assert() end
      param = record.params[record.paramsLen]
      record.paramsLen = record.paramsLen + 1
      param.target = targetID
      param.num = 0
  end

	if param.num < needNum then
		param.num = param.num  + 1
		local gcTaskObjNumMsg = Msg.gc.GC_TASK_OBJ_NUM
		gcTaskObjNumMsg.taskID = taskID
		gcTaskObjNumMsg.objTypeID = targetID
		gcTaskObjNumMsg.objNum = param.num 
		Msg.send(gcTaskObjNumMsg, human.fd)
		
		if param.num  == needNum then
			taskChangeFinish(human, taskID, true)
		end
	end
end 

function loginFixMonsterTarget(human, record)
  local taskConfig = TaskHelp.getTaskConfig(record.taskid)
  local taskType2 = TaskAction.getTaskType2(taskConfig)
  if taskType2 == TaskDefine.TASK_TYPE_2_MONSTER_TARGET or taskType2 == TaskDefine.TASK_TYPE_2_COLLECT then
      for targetID, targetNum in pairs(taskConfig.targetParam) do          
            if not isTargetFinish(human, record, targetID, targetNum) then
              return
            end
      end
      record.finishFlag = TaskDefine.TASK_FINISH_FLAG_CAN_FINISH
  end
end

-- 怪物被打死回调
function taskMonsterDieCB(human, killType, monster, monsterID)
	for i = 0, human.db.taskRecordLen-1 do
	  
	  local record = human.db.taskRecord[i]
	  local taskID = record.taskid
		if record.status == TaskDefine.TASK_ACCEPTED then
			local taskConfig = TaskHelp.getTaskConfig(taskID)
			local taskType2 = TaskAction.getTaskType2(taskConfig)
			
--			print("taskMonsterDieCB killType:", killType, " configtype:", taskConfig.targetShare, " taskID:", taskID)
			
			if taskConfig.targetShare >= killType then
				-- 判断任务对象
				if taskType2 == TaskDefine.TASK_TYPE_2_MONSTER_TARGET then -- 杀死指定ID怪物计数
					local changedToFinish = true
					local inTask = nil
					for targetID, targetNum in pairs(taskConfig.targetParam) do
						if targetID == monsterID then
							if updateTaskObjNum(human, record, taskID, monsterID, targetNum) then
							   inTask = true
							end
						end						
						if not isTargetFinish(human, record, targetID, targetNum) then
							changedToFinish = false
						end
					end
					if changedToFinish == true and inTask == true then
						taskChangeFinish(human, taskID, true)
					end
				elseif taskType2 == TaskDefine.TASK_TYPE_2_MONSTER_LEVEL then -- 杀死特定等级范围怪物计数
					local lv = ObjMonster.getLv(monster) 
					local lvLow = taskConfig.targetParam[2]
					local lvHigh = taskConfig.targetParam[3]
					local targetNum = taskConfig.targetParam[1]		
					if lvHigh >= lv and lvLow <= lv then
						updateTaskObjNumSpecial(human, record, taskID, 0, targetNum)
					end
				elseif taskType2 == TaskDefine.TASK_TYPE_2_MONSTER_TITLE then -- 杀死特定官阶怪物计数
					local guanjie = ObjMonster.get_conf(monster).monster_type
					local targetGuanjie = taskConfig.targetParam[2]
					local targetNum = taskConfig.targetParam[1]
					if targetGuanjie == guanjie then
						updateTaskObjNumSpecial(human, record, taskID, targetGuanjie, targetNum)
						break
					end
				elseif taskType2 == TaskDefine.TASK_TYPE_2_MONSTER_COLLECT then -- 杀怪采集任务
					local changedToFinish = true
					local inTask = nil
					for targetID, targetNum in pairs(taskConfig.targetParam.m) do
						if targetID == monsterID then
							if updateTaskObjNum(human, record, taskID, monsterID, targetNum) then
							   inTask = true
							end
						end
						if not isTargetFinish(human, record, targetID, targetNum) then
							changedToFinish = false
						end
					end
					if inTask == true then
						for targetID, targetNum in pairs(taskConfig.targetParam.c) do
							if not isTargetFinish(human, record, targetID, targetNum) then
								changedToFinish = false
								break
							end
						end
					end
					if changedToFinish == true and inTask == true then
						taskChangeFinish(human, taskID, true)
					end
				end
			end
		end
	end
end

-- 采集物被采集回调
function taskCollectCB(human, collect, collectID)
  for i = 0, human.db.taskRecordLen-1 do
    
    local record = human.db.taskRecord[i]
    local taskID = record.taskid
		if record.status == TaskDefine.TASK_ACCEPTED then
			local taskConfig = TaskHelp.getTaskConfig(taskID)
			local taskType2 = TaskAction.getTaskType2(taskConfig)
			-- 判断任务对象
			if taskType2 == TaskDefine.TASK_TYPE_2_COLLECT then -- 采集指定ID的采集物计数
				local changedToFinish = true
				local inTask = false
				for targetID, targetNum in pairs(taskConfig.targetParam) do
					if targetID == collectID then
						if updateTaskObjNum(human, record, taskID, collectID, targetNum) then
						    inTask = true
						end
					end		
					if not isTargetFinish(human, record, targetID, targetNum) then
						changedToFinish = false
					end
				end
				if changedToFinish == true and inTask == true then
					taskChangeFinish(human, taskID, true)
				end				
			elseif taskType2 == TaskDefine.TASK_TYPE_2_MONSTER_COLLECT then -- 杀怪采集任务
				local changedToFinish = true
				local inTask = false
				for targetID, targetNum in pairs(taskConfig.targetParam.c) do
					if targetID == collectID then
						 if updateTaskObjNum(human, record, taskID, collectID, targetNum) then
						    inTask = true
						 end
					end		
					if not isTargetFinish(human, record, targetID, targetNum) then
						changedToFinish = false
					end
				end
				if inTask == true then
					for targetID, targetNum in pairs(taskConfig.targetParam.m) do
						if not isTargetFinish(human, record, targetID, targetNum)then
							changedToFinish = false
							break
						end
					end
				end
				if changedToFinish == true and inTask == true then
					taskChangeFinish(human, taskID, true)
				end
			end
		end
	end
end

-- 使用道具回调
function taskUseItemCB(human, itemID)
    for i = 0, human.db.taskRecordLen - 1 do
      local record = human.db.taskRecord[i]
      local taskID = record.taskid
      if record.status == TaskDefine.TASK_ACCEPTED and record.finishFlag == TaskDefine.TASK_FINISH_FLAG_NONE  then
			   local taskConfig = TaskHelp.getTaskConfig(taskID)
			   local taskType2 = TaskAction.getTaskType2(taskConfig)
			   if taskType2 == TaskDefine.TASK_TYPE_2_USE_ITEM then -- 使用道具完成任务目标
				  if itemID == taskConfig.targetParam[1] then
					 if taskConfig.targetParam[2] == nil then
						taskChangeFinish(human, taskID, true)
					 else
						local mapID = taskConfig.targetParam[2]
						local mapX = taskConfig.targetParam[3]
						local mapY = taskConfig.targetParam[4]
						local radius = taskConfig.targetParam[5]
						if mapID == Map.scene_id_to_map_id[human.scene_id] then
							local distLen = Util.dist(mapX, mapY, human.x, human.y)
							if distLen <= radius then
								taskChangeFinish(human, taskID, true)
							end
						end					
					 end
				  end
			 end
		  end
	 end
end

--锻造操作回调(无部位要求)
function taskRefineCB(human, refineOpID, pos)
  for i = 0, human.db.taskRecordLen-1 do    
    local record = human.db.taskRecord[i]
    local taskID = record.taskid    
		if record.status == TaskDefine.TASK_ACCEPTED and record.finishFlag == TaskDefine.TASK_FINISH_FLAG_NONE  then
			local taskConfig = TaskHelp.getTaskConfig(taskID)
			local taskType2 = TaskAction.getTaskType2(taskConfig)
			if taskType2 == TaskDefine.TASK_TYPE_2_REFINE and taskConfig.targetParam[1] == refineOpID then 
				updateTaskObjNum(human, record, taskID, refineOpID, taskConfig.targetParam[2])        
        if isTargetFinish(human, record, refineOpID, taskConfig.targetParam[2]) then
          taskChangeFinish(human, taskID, true)
        end
      elseif taskType2 == TaskDefine.TASK_TYPE_2_REFINE_POS and pos and taskConfig.targetParam[1] == refineOpID and taskConfig.targetParam[3] == pos then
        updateTaskObjNum(human, record, taskID, refineOpID, taskConfig.targetParam[2])        
        if isTargetFinish(human, record, refineOpID, taskConfig.targetParam[2]) then
          taskChangeFinish(human, taskID, true)
        end
			end
		end
	end
end


-- 玩家被打死回调
function taskRoleDieCB(human, dier, actOpID)
	if not human or human.obj_type ~= Obj.TYPE_HUMAN then
		return
	end
	for taskID, record in pairs(human.db.taskRecord) do
		if record.status == TaskDefine.TASK_ACCEPTED and record.finishFlag == TaskDefine.TASK_FINISH_FLAG_NONE then
			local taskConfig = TaskHelp.getTaskConfig(taskID)
			local taskType2 = TaskAction.getTaskType2(taskConfig)
			if taskType2 == TaskDefine.TASK_TYPE_2_ACT and taskConfig.targetParam[1] == actOpID then 
				updateTaskObjNum(human, record, taskID, actOpID, taskConfig.targetParam[2])
				if record[actOpID] and record[actOpID] >= taskConfig.targetParam[2] then
					taskChangeFinish(human, taskID, true)
				end
			end
		end
	end
end


-- 好友互动回调
function taskFriendCB(human, friendOpID)
 for i = 0, human.db.taskRecordLen-1 do    
    local record = human.db.taskRecord[i]
    local taskID = record.taskid
		if record.status == TaskDefine.TASK_ACCEPTED and record.finishFlag == TaskDefine.TASK_FINISH_FLAG_NONE then
			local taskConfig = TaskHelp.getTaskConfig(taskID)
			local taskType2 = TaskAction.getTaskType2(taskConfig)
			if taskType2 == TaskDefine.TASK_TYPE_2_FRIEND and taskConfig.targetParam[1] == friendOpID then 
				updateTaskObjNum(human, record, taskID, friendOpID, taskConfig.targetParam[2])
				if record[friendOpID] and record[friendOpID] >= taskConfig.targetParam[2] then
					taskChangeFinish(human, taskID, true)
				end
			end
		end
	end
end

-- 功能交互回调
function taskInteractCB(human, interOpID)
  for i = 0, human.db.taskRecordLen-1 do    
    local record = human.db.taskRecord[i]
    local taskID = record.taskid
		if record.status == TaskDefine.TASK_ACCEPTED and record.finishFlag == TaskDefine.TASK_FINISH_FLAG_NONE then
			local taskConfig = TaskHelp.getTaskConfig(taskID)
			local taskType2 = TaskAction.getTaskType2(taskConfig)			
			if taskType2 == TaskDefine.TASK_TYPE_2_INTERACT and taskConfig.targetParam[1] == interOpID then 
				updateTaskObjNum(human, record, taskID, interOpID, taskConfig.targetParam[2])				
				if isTargetFinish(human, record, interOpID, taskConfig.targetParam[2]) then
					taskChangeFinish(human, taskID, true)
				end
			end
		end
	end
end

-- 爬塔功能回调
function taskPataCB(human,  pataMax)
  for i = 0, human.db.taskRecordLen-1 do    
    local record = human.db.taskRecord[i]   
    local taskID = record.taskid
    if record.status == TaskDefine.TASK_ACCEPTED and record.finishFlag == TaskDefine.TASK_FINISH_FLAG_NONE then
      local taskConfig = TaskHelp.getTaskConfig(taskID)
      local taskType2 = TaskAction.getTaskType2(taskConfig)      
      if taskType2 == TaskDefine.TASK_TYPE_2_INTERACT and taskConfig.targetParam[1] == TaskDefine.TASK_INTERACT_PATA then 
        updateTaskObjNum(human, record, taskID, TaskDefine.TASK_INTERACT_PATA, taskConfig.targetParam[2], pataMax)        
        if isTargetFinish(human, record, TaskDefine.TASK_INTERACT_PATA, taskConfig.targetParam[2]) then
          taskChangeFinish(human, taskID, true)
        end
      end
    end 
  end
end


-- 副本进入回调
function taskCopyEnterCB(human, copyID)
  for i = 0, human.db.taskRecordLen-1 do    
     local record = human.db.taskRecord[i]
     local taskID = record.taskid
      if record.status == TaskDefine.TASK_ACCEPTED and record.finishFlag == TaskDefine.TASK_FINISH_FLAG_NONE  then
			local taskConfig = TaskHelp.getTaskConfig(taskID)
			local taskType2 = TaskAction.getTaskType2(taskConfig)
			if taskType2 == TaskDefine.TASK_TYPE_2_COPY_MULTI then
				for i = 1, #taskConfig.targetParam do
					if copyID == taskConfig.targetParam[i] then
						taskChangeFinish(human, taskID, true)
						break
					end
				end
			end
		end
	end
end

-- 副本完成回调
function taskCopyCB(human, copyID)
  for i = 0, human.db.taskRecordLen-1 do    
     local record = human.db.taskRecord[i]
     local taskID = record.taskid
     if record.status == TaskDefine.TASK_ACCEPTED and record.finishFlag == TaskDefine.TASK_FINISH_FLAG_NONE  then
			local taskConfig = TaskHelp.getTaskConfig(taskID)
			local taskType2 = TaskAction.getTaskType2(taskConfig)
			if taskType2 == TaskDefine.TASK_TYPE_2_COPY and taskConfig.targetParam[1] == copyID then 
				updateTaskObjNum(human, record, taskID, copyID, taskConfig.targetParam[2])
				if isTargetFinish(human, record, copyID, taskConfig.targetParam[2]) then
					taskChangeFinish(human, taskID, true)
				end
			end
		end
	end
end

-- 搜索任务到达某区域
function taskReachPlaceCB(human, taskID)
	local taskConfig = TaskHelp.getTaskConfig(taskID)
	if taskConfig.target ~= TaskDefine.TASK_TYPE_2_REACH_AREA_MULTI then return end
	local record = human.db.taskRecord[taskID]
	if record == nil then return end
	if record.status ~= TaskDefine.TASK_ACCEPTED or record.canFinish == true then return end
		
	local finishNum = 0
	local changed = nil
	for i = 1, #taskConfig.targetParam do
		if record[i] == nil then 
			local mapID = taskConfig.targetParam[i][1]
			local mapX = taskConfig.targetParam[i][2]
			local mapY = taskConfig.targetParam[i][3]
			local radius = taskConfig.targetParam[i][4]
		
			if mapID == Map.scene_id_to_map_id[human.scene_id] then
				if Util.dist(mapX, mapY, human.x, human.y) <= radius then
					updateTaskObjNum(human, record, taskID, i, 1)			
					changed = true
					finishNum = finishNum + 1
				end
			end
		else
			finishNum = finishNum + 1
		end
	end
	
	if changed and finishNum == #taskConfig.targetParam then
		taskChangeFinish(human, taskID, true)
	end
end
